package cn.com.huzo.service.view.master;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import static org.apache.poi.ss.util.SheetUtil.getCell;
import org.springframework.web.servlet.view.document.AbstractXlsView;

/**
 * 导出材料表Excel
 * <p>
 * @author 范学春
 */
@SuppressWarnings("unchecked")
public class MaterialExcelView extends AbstractXlsView {

    public MaterialExcelView() {
        super();
    }

    @Override
    protected Workbook createWorkbook(Map<String, Object> model, HttpServletRequest request) {
        HSSFWorkbook workbook = null;
        try (InputStream _tmp = new FileInputStream(request.getServletContext().getRealPath("/") + "/WEB-INF/templates/80_master/材料信息一览表（模版）.xls")) {
            workbook = new HSSFWorkbook(_tmp);
        } catch (FileNotFoundException ex) {
            //Logger.getLogger(ProductionPlanningExcelView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            //Logger.getLogger(ProductionPlanningExcelView.class.getName()).log(Level.SEVERE, null, ex);
        }

        return workbook;
    }

    /**
     *
     * @param model 文件数据
     * @param workbook 基于预算成本表格（模版）.xls创建的excel文件
     * @param request 客户端请求
     * @param response 客户端响应
     * @throws Exception
     */
    @Override
    protected void buildExcelDocument(Map<String, Object> model,
            Workbook workbook, HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
        int _startRow = 4;   // 起始明细行

        //
        //显示对话框提示用户下载或打开
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String("材料一览表.xls".getBytes(), "ISO8859-1"));

        // 从模版取得第一个sheet
        Sheet sheet = workbook.getSheetAt(0);

        // 边框样式
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom(BorderStyle.THIN);     // 下边框    
        cellStyle.setBorderLeft(BorderStyle.THIN);       // 左边框    
        cellStyle.setBorderTop(BorderStyle.THIN);        // 上边框    
        cellStyle.setBorderRight(BorderStyle.THIN);      // 右边框 

        // 报表头
        Object commtobj = model.get("commentlist");
        String commtstr = commtobj.toString();
        String[] commtArr = commtstr.split(",");
        for (Integer i = 0; i < commtArr.length; i++) {
            getCell(sheet, 3, i).setCellStyle(cellStyle);
            getCell(sheet, 3, i).setCellValue(commtArr[i]);
        }

        Object obj0 = model.get("materialtype");
        getCell(sheet, 2, 0).setCellValue(obj0.toString());

        //明细
        Object obj = model.get("lineslist");
        //      List objArr=new ArrayList();
        //      objArr.add(obj);
        List<String> objArr = (ArrayList) obj;

        //创建新行
        _startRow = 4;   // 起始明细行
        String Arrstr1 = objArr.get(0);
        String[] linesArr1 = Arrstr1.split(",");
        for (Integer j = 0; j < linesArr1.length; j++) {
            sheet.shiftRows(_startRow, sheet.getLastRowNum(), 1);
            Row newRow = sheet.createRow(_startRow);
            _startRow = _startRow + 1;
        }

        //明细赋值 
        for (Integer i = 0; i < objArr.size(); i++) {
            String Arrstr = objArr.get(i);
            String[] linesArr = Arrstr.split(",");

            _startRow = 4;   // 起始明细行
            for (Integer j = 0; j < linesArr.length; j++) {
                String value1 = "";
                if (!linesArr[j].contains("null") && linesArr[j] != null) {
                    value1 = linesArr[j];
                }
                Row sourceRow = sheet.getRow(_startRow);
                Cell newCell = sourceRow.createCell(i);
                newCell.setCellStyle(cellStyle);
                getCell(sheet, _startRow, i).setCellValue(value1);
                _startRow = _startRow + 1;
            }
        }
    }
}
